package com.supersit.greensproject.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.model.PolygonOptions;
import com.amap.api.maps2d.model.PolylineOptions;
import com.supersit.greensproject.R;
import com.supersit.greensproject.entity.Coordinate;
import com.supersit.greensproject.view.MyTitleBar;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * 高德地圖顯示基地的區域形狀
 * Created by cong on 2016/7/15.
 */
@ContentView(R.layout.activity_amap_polygon)
public class AMapPolygonActivity extends BaseActivity {
    private Context context;
    @ViewInject(R.id.mv_amap)
    private MapView mapView;
    @ViewInject(R.id.x)
    private TextView xx;
    @ViewInject(R.id.y)
    private TextView yy;
    private AMap aMap;
    private List<LatLng> latLngsList;
    private List<Coordinate> coordinates;
    private PolygonOptions polygonOptions;
    //标题栏
    private MyTitleBar myTitleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        context = this;
        mapView.onCreate(savedInstanceState);
        initVariable();
        bindListener();
        init();
    }

    private void initVariable() {
        setMyTitleBar();
        Bundle bundle = getIntent().getExtras();
        coordinates = (List<Coordinate>) bundle.getSerializable("coordinates");
    }

    private void setMyTitleBar() {
        myTitleBar = new MyTitleBar(context, this.findViewById(R.id.mv_amap_title));
        myTitleBar.setTitleText(getResources().getString(R.string.show_map_polygon));
        myTitleBar.setChildVisibility(false, myTitleBar.getmImgRight());
    }

    private void bindListener() {
        myTitleBar.setImg1Listener(onClickListener);


    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.title_bar_left:
                    finish();
                    break;
            }
        }
    };

    /**
     * 初始化
     */
    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
            if (null != coordinates) {
                latLngsList = new ArrayList<LatLng>();
                for (int i = 0; i < coordinates.size(); i++) {
                    LatLng latLng = new  LatLng(coordinates.get(i).getLatitude(), coordinates.get(i).getLongitude());
                    MarkerOptions markerOptions = new MarkerOptions();//地图标记点
                    markerOptions.draggable(false);//标记点是否可以拖动
                    markerOptions.position(latLng);
                    markerOptions.title(""+i);
                    markerOptions.visible(true);//標記點是否可見
                   Marker marker =  aMap.addMarker(markerOptions);
                    marker.showInfoWindow();
                    latLngsList.add(latLng);
                }
                latLngsList.add(latLngsList.get(0));
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLngsList.get(0), 20));

            }
            aMap.setOnCameraChangeListener(onCameraChangeListener);
        }
        initPolyLine(latLngsList);
        //randomPolyline();//随机生成多边形
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，实现地图生命周期管理
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，实现地图生命周期管理
        mapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，实现地图生命周期管理
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 生成一个长方形的四个坐标点
     */
    private List<LatLng> createRectangle(LatLng center, double halfWidth,
                                         double halfHeight) {
        return Arrays.asList(new LatLng(center.latitude - halfHeight,
                        center.longitude - halfWidth), new LatLng(center.latitude
                        - halfHeight, center.longitude + halfWidth), new LatLng(
                        center.latitude + halfHeight, center.longitude + halfWidth),
                new LatLng(center.latitude + halfHeight, center.longitude
                        - halfWidth));
    }

    private AMap.OnCameraChangeListener onCameraChangeListener = new AMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {
            LatLng target = cameraPosition.target;
            xx.setText(target.longitude + "");
            yy.setText(target.latitude + "");
        }

        @Override
        public void onCameraChangeFinish(CameraPosition cameraPosition) {

        }
    };

    private void initPolyLine(List<LatLng> latlngs) {
        PolylineOptions polylineOptions = new PolylineOptions();
        polylineOptions.color(Color.BLUE);//线段颜色
        polylineOptions.setDottedLine(false);//是否是虚线
        polylineOptions.visible(true);//是否在地图上显示
        polylineOptions.width(2f);//线段宽度
        polylineOptions.addAll(latlngs);
        aMap.addPolyline(polylineOptions);
    }

    private void randomPolyline() {
        Random random = new Random(100);
        LatLng start = new LatLng(23.156846, 113.276846);
        List<LatLng> points = new ArrayList<LatLng>();
        int i = 0;
        while (i < 5) {
            double lat = random.nextDouble() * 0.1;
            double lng = random.nextDouble() * 0.1;
            LatLng latLng = new LatLng(start.latitude + lat, start.longitude + lng);
            points.add(latLng);
            i++;
        }
        points.add(points.get(0));
        PolylineOptions options = new PolylineOptions();
        options.color(Color.BLUE);//线段颜色
        options.setDottedLine(false);//是否是虚线
        options.visible(true);//是否在地图上显示
        options.width(1f);//线段宽度
        options.addAll(points);
        aMap.addPolyline(options);
    }
}
